require "Common/define"
require "Logic/Config"
require "Common/functions"

HeadRectDrawItem ={}
HelperFunc.SetUIEnv(HeadRectDrawItem)
local this = HeadRectDrawItem;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;
this.quality = nil;
this.icon = nil;
this.iconRound = nil;
this.qualityFrame = nil;
this.name = nil;
this.lv = nil;
this.isPool = nil;
this.drawRate = nil;
this.typeIcon = nil;
---@type GList
this.starList = nil
---@type GController
this.ctrlIcon = nil
function HeadRectDrawItem.Init()
    local name = getUIName(PanelNames.HeadRectDrawItem);
    panelMgr:ShowUI(name, this.onCreate,Config.useLocalUI);
end

function HeadRectDrawItem.onCreate(obj)
    obj:SetActive(false);
end
---@param obj GComponent
function HeadRectDrawItem.initData(obj,data,is)
    this.isPool = is;
    --LuaBehaviour = luaBehaviour;
    --gameObject = obj;
    --transform = obj.transform;
    obj.onClick:Clear()
    this.quality = obj:GetChild("framebg")
    this.qualityFrame=obj:GetChild("frame")
    this.icon = obj:GetChild("icon"):GetChild("icon");
    this.name = obj:GetChild("name")
    this.lv = obj:GetChild("level")
    this.drawRate=obj:GetChild("drawRate")
    this.iconRound=obj:GetChild("bgRound_icon")
    --starList = obj:GetChild("star")
    this.ctrlIcon = obj:GetController("ctrlIcon")
    this.topicon = obj:GetController("topicon")
    this.typeIcon = obj:GetChild("outplacement");
    local heroTypeIcon = EnumConst.HeroTypeIcon[data["type"]]
    if heroTypeIcon then
        this.typeIcon.url = "ui://ICONAtlas/" .. heroTypeIcon
    end

    --[[local table_Star = {};
    for i = 1, 6 do
        local star = transform:Find("StarPanel/star"..i):GetComponent("Image");
        table.insert(table_Star,star);
    end]]

    local table_heroConfig = G.dataTable["t_hero"] or {};
    local curHeroData;
    curHeroData=table_heroConfig[tostring(data["partnerModelId"])];
    if(curHeroData==nil)then
        return
    end
    this.lv.text="";
    this.drawRate.text="";
    this.name.text="";
    if data["round"]~=nil then
        this.ctrlIcon.selectedIndex = 1
        --starList.visible = false
        --iconRound.url =  EnumConst.AtlasPath.ICONAtlas..curHeroData.f_HeroIcon
        --icon.transform.parent.gameObject:SetActive(false);
        --iconRound.transform.parent.gameObject:SetActive(true);
        --transform:GetComponent("Image").color=Color(1,1,1,0.006);
        --table_Star[1].transform.parent.gameObject:SetActive(false);
        --resMgr:LoadIconFromPrefab("ICONAtlas",curHeroData.f_HeroIcon,function(sp)
        --    iconRound.sprite = sp;
        --end);
        --lv.text="";
        --drawRate.text="";
        --name.text="";
        return;
    end
    this.name.text = getLanguage(curHeroData.f_HeroName);
    this.icon.url = EnumConst.AtlasPath.ICONAtlas..curHeroData.f_HeroIcon
    if data.skinId and tonumber(data.skinId) > 0 then
        this.icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(data.skinId);
    end
    local tempstar=logicMgr.HeroManager.starToQuality(data["star"]);
    if this.isPool then
        local star = logicMgr.HeroManager.GetHeroConfigbyModelId(data.partnerModelId).f_HeroInitialStar;
        local qualityIcon = logicMgr.HeroManager.starToQuality(star);

        this.qualityFrame.url = "ui://ICONAtlas/" .. Config.IconFrame[qualityIcon]
        this.quality.url = "ui://ICONAtlas/" .. Config.IconQuality[qualityIcon]
    else
        this.quality.url = EnumConst.AtlasPath.ICONAtlas..Config.IconQuality[tempstar]
        this.qualityFrame.url = EnumConst.AtlasPath.ICONAtlas..Config.IconFrame[tempstar]
    end
    --[[resMgr:LoadIconFromPrefab("ICONAtlas",curHeroData.f_HeroIcon,function(sp)
        this.icon.sprite = sp;
    end);
    local tempstar=logicMgr.HeroManager.starToQuality(data["star"]);
    resMgr:LoadIconFromPrefab("ICONAtlas",Config.IconQuality[tempstar],function(sp)
        quality.sprite = sp;
    end);
    resMgr:LoadIconFromPrefab("ICONAtlas",Config.IconFrame[tempstar],function(sp)
        qualityFrame.sprite = sp;
    end);]]

    if data["hide"]~=nil then
        this.ctrlIcon.selectedIndex = 0
        --starList.visible = false
        --[[this.icon.transform.parent.gameObject:SetActive(true);
        iconRound.transform.parent.gameObject:SetActive(false);
        quality.gameObject:GetComponent("RectTransform").anchoredPosition=Vector2.zero;
        transform:GetComponent("Image").color=Color(1,1,1,0.006);
        --transform:GetComponent("RectTransform").sizeDelta = Vector2(170, 170);
        table_Star[1].transform.parent.gameObject:SetActive(false);
        lv.text="";
        drawRate.text="";
        name.text="";]]
        return;
    end
    local showStar=logicMgr.HeroManager.starToShow(data["star"]);
    local cutShow=0;
    if showStar ~= nil then
        --starList:RemoveChildrenToPool()
        --for i = 1, showStar do
        --    cutShow=cutShow+1;
        --    local star = starList:AddItemFromPool()
        --    star:GetController("cstar").selectedIndex = 1
        --end
        --if(showStar-cutShow==0.5)then
        --    local star = starList:AddItemFromPool()
        --    star:GetController("cstar").selectedIndex = 2
        --end
        ----[[for i = 1, 6 do
        --    table_Star[i].gameObject:SetActive(false);
        --end
        --for i = 1, showStar do
        --    cutShow=cutShow+1;
        --    table_Star[i].gameObject:SetActive(true);
        --    table_Star[i].fillAmount=1;
        --end
        --if(showStar-cutShow==0.5)then
        --    table_Star[cutShow+1].gameObject:SetActive(true);
        --    table_Star[cutShow+1].fillAmount=0.5;
        --end]]
    end

    if data["cube"]~=nil then
        this.ctrlIcon.selectedIndex = 0
        --starList.visible = false
        --[[this.icon.transform.parent.gameObject:SetActive(true);
        iconRound.transform.parent.gameObject:SetActive(false);
        quality.gameObject:GetComponent("RectTransform").anchoredPosition=Vector2.zero;
        transform:GetComponent("Image").color=Color(1,1,1,0.006);
        --transform:GetComponent("RectTransform").sizeDelta = Vector2(170, 170);
        --table_Star[1].transform.parent.gameObject:SetActive(false);
        lv.text="";
        drawRate.text="";
        name.text="";]]
        return;
    end

    this.lv.visible = data["level"] ~= nil
    if data["level"] ~= nil then
        this.lv.text = string.format(getLanguage("Level"), data["level"]);
    end
    if(data["drawRate"]~=nil)then
        this.drawRate.text = getLanguage("Sys_Chance").."%"..(data["drawRate"]*100);
    end
    if this.topicon and data.faction then
        this.topicon.selectedIndex = data.faction;
    end
end


return HeadRectDrawItem;

